﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Reflection;

public partial class View_GV2XLS : ViewMatrix //System.Web.UI.Page
{
    DataTable dt = null;
    Table xlsTable = null;
    string caption
    {
        get
        {
            string cc = Request.QueryString["caption"] as string;
            cc = (cc == null) ? "" : cc;
            return cc;
        }
    }
    string captionRight
    { 
        get
        {
            string rr  = Request.QueryString["captionRight"] as string;
            rr = (rr == null) ? "" : rr;
            return rr;
        }
    }
    string toHtmlMethod
    {
        get
        {
            string rr = Request.QueryString["toHtmlMethod"] as string;
            rr = (rr == null) ? "" : rr;
            return rr;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetData();
            if (captionRight == string.Empty)
            { Trans2EXCEL(); }
            else
            { trans2ExcelWithHtmlTable(toHtmlMethod); }
            
        }
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }

    protected void GetData()
    {
        string strMethod;
        strMethod = Request.QueryString["method"] as string;
        serviceName =string.Format("MISAPP.Classify.Service.{0}",Request.QueryString["service"] );
        if (strMethod == null)
        { return; }
              
        dt = GetDataFromSerevice(strMethod);
        GV2XLS.Caption = caption;
        GV2XLS.DataSource = dt;
        GV2XLS.DataBind();

       
    }

    protected void Trans2EXCEL()
    {
        if (dt == null)
        { return; }

        string excelFileName = Request.QueryString["xls"] as string;
        if (excelFileName == null)
        { return; }


        Response.ClearContent();
        Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");
        Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", Server.UrlEncode(excelFileName)));
        Response.ContentType = "application/excel";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GV2XLS.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }

    protected void wmg002ToHtmlTable()
    {
        int borderWidth = 1;
        if (GV2XLS.Rows.Count < 0)
        { return; }
        xlsTable = new Table();
        //xlsTable.BorderWidth = 1;

        //caption row
        TableRow tbRow = customizeCaptionRow(
            dt.Columns.Count, caption, captionRight);
        xlsTable.Rows.Add(tbRow);
        tbRow = new TableRow();
        //foreach (TableCell cll in GV2XLS.HeaderRow.Cells)
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            TableCell colCell = new TableCell();
            colCell.Text = dt.Columns[i].ColumnName;
            colCell.BorderWidth = borderWidth;
            tbRow.Cells.Add(colCell);
        }
        xlsTable.Rows.Add(tbRow);
        

        //body
        bool isBorder0px = false;
        foreach (DataRow row in dt.Rows)
        {
            tbRow = new TableRow();
            //foreach (TableCell cll in row.Cells)
            for (int i = 0; i < dt.Columns.Count ; i++)
            {
                TableCell colCell = new TableCell();

                switch (row[3].ToString())
                {
                    case "台幣活期":
                    case "台幣定期":
                    case "台幣活存比":
                        isBorder0px = true;
                        break;
                    default:
                        if (isBorder0px == false)
                        {
                            colCell.BorderWidth = borderWidth;
                        }
                        break;
                }
                colCell.Text = row[i].ToString();
                tbRow.Cells.Add(colCell);
            }
            xlsTable.Rows.Add(tbRow);
        }

        if (dt == null)
        { return; }

        string excelFileName = Request.QueryString["xls"] as string;
        if (excelFileName == null)
        { return; }


        Response.ClearContent();
        Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");
        Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", Server.UrlEncode(excelFileName)));
        Response.ContentType = "application/excel";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        xlsTable.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }
   
    protected TableRow customizeCaptionRow(int p_headLength,string p_left,string p_right)
    {
        TableRow tbRow = new TableRow();
        int leftColSpan = p_headLength - 2;
        TableCell leftCell = new TableCell();
        leftCell.ColumnSpan = p_headLength - 2;
        leftCell.HorizontalAlign = HorizontalAlign.Left;
        leftCell.Text = p_left;
        tbRow.Cells.Add(leftCell);

        TableCell rightCell = new TableCell();
        rightCell.ColumnSpan = 2;
        rightCell.HorizontalAlign = HorizontalAlign.Right;
        rightCell.Text = p_right;
        tbRow.Cells.Add(rightCell);

        return tbRow;
    }

    protected void trans2ExcelWithHtmlTable(string p_method)
    {
        Type t = this.GetType(); 
        MethodInfo mi = t.GetMethod(p_method
            , BindingFlags.Instance |BindingFlags.Public |
              BindingFlags.NonPublic |BindingFlags.FlattenHierarchy);

        mi.Invoke(this, null);  
    }
}
